from utils import xprimes

primes = set([str(i) for i in xprimes(10**6)])

def search_prefix(prefix, depth, cut):
    if depth >= cut:
        return set([prefix])
    ret = set()
    for i in xrange(1, 10):
        next = prefix + str(i)
        if next in primes:
            ret |= search_prefix(next, depth+1, cut)
    return ret 

def search_suffix(suffix, depth, cut):
    if depth >= cut:
        return set([suffix])
    ret = set()
    for i in xrange(1, 10):
        next = str(i) + suffix
        if next in primes:
            ret |= search_suffix(next, depth+1, cut)
    return ret 

truns = set()
cut = 2
while True:
    if len(truns) >= 11:
        break
    truns |= (search_prefix('', 0, cut) & search_suffix('', 0, cut))
    cut += 1

print sum(map(int, truns))
